#! perl

use Glib;
use Glib::CodeGen;
use Module::Build 0.24;
use ExtUtils::ParseXS;

# Find Glib library location
for (@DynaLoader::dl_shared_objects) {
  if (/Glib/) {
    $GlibLoc = $_;
    $GlibLoc =~ s#/[^/]+$##;
    last;
  }
}

Glib::CodeGen->parse_maps("oscats", input => "oscats.maps",
        header => 'lib/oscats-autogen.h', 
        typemap => 'lib/oscats.typemap',
        register => 'lib/register-oscats.xsh',
);

$glib_loc = $INC{"Glib/CodeGen.pm"};
$glib_loc =~ s#CodeGen\.pm$#Install#;
$glib_typemap = "$glib_loc/typemap";

ExtUtils::ParseXS::process_file(
  filename => 'lib/oscats.xs',
  typemap => [ 'oscats.typemap', 'oscats-typedefs.typemap', $glib_typemap ],
  prototypes => 0,
  output => 'lib/oscats.c');

$build = Module::Build->new
  (
    module_name => "oscats",
    dist_abstract => "Open-Source Computerized Adaptive Testing System",
    license => "gpl",
    build_requires => { "ExtUtils::CBuilder" => 0, },
    requires => { "perl" => 5.008,
                  "GLib" => 0, },
    extra_compiler_flags => " @GLIB_CFLAGS@ @GSL_CFLAGS@ @CPPFLAGS@ @CFLAGS@ ",
    extra_linker_flags => " -L$GlibLoc -lGlib -L$ENV{top_builddir}/src/liboscats/.libs -loscats " .
                          " @LDFLAGS@ @GLIB_LIBS@ @GSL_LIBS@ ",
    include_dirs => [ "$ENV{top_srcdir}/src/liboscats", $glib_loc, ],
    install_base => "@prefix@",
  );

$build->create_build_script;
